package com.fysports.framework.render;

import com.jfinal.log.Log;
import com.jfinal.render.JsonRender;
import com.fysports.web.module.view.JsonView;


/**
 * 统计Json渲染时间
 * Created by konbluesky
 * Date : 14-9-12 下午8:06
 * Project : JdmData_WebServer
 */
public class TimerJsonRender extends JsonRender {
    private static Log logger = Log.getLog(TimerJsonRender.class);
    private JsonView jsonview;


    public TimerJsonRender(JsonView jv) {
        super(jv.toJson());
        jsonview = jv;
    }


    @Override public void render() {
        long start = System.currentTimeMillis();
        /*
        * json渲染，将逻辑处理结果也记入统计
        * jsonview status
        * */
        String flag =
                jsonview.isStatus() ? MainRenderFactory.JSON_SUCCESS : MainRenderFactory.JSON_FAIL;
        try {
            super.render();
        }
        catch (Exception e) {
            logger.error("Json渲染失败:", e);
            flag = MainRenderFactory.JSON_ERROR;
            throw e;
        }finally {
            long end = System.currentTimeMillis();
            long renderTime = end - start;
            request.setAttribute(MainRenderFactory.renderFlag, flag);
            request.setAttribute(MainRenderFactory.renderTimeKey, renderTime);
        }
    }

}
